package cn.ihoway;

import cn.ihoway.dao.CharacterDao;
import cn.ihoway.entity.Character;
import cn.ihoway.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;

public class WebDownload {
    public static void main(String[] args) throws IOException {
        String nameStr = "七七\n" +
                "刻晴\n" +
                "琴\n" +
                "莫娜\n" +
                "迪卢克\n" +
                "温迪\n" +
                "可莉\n" +
                "达达利亚\n" +
                "钟离\n" +
                "阿贝多\n" +
                "甘雨\n" +
                "魈\n" +
                "胡桃\n" +
                "优菈\n" +
                "枫原万叶\n" +
                "神里绫华\n" +
                "宵宫\n" +
                "雷电将军\n" +
                "珊瑚宫心海\n" +
                "荒泷一斗\n" +
                "申鹤\n" +
                "八重神子\n" +
                "神里绫人\n" +
                "夜兰\n" +
                "提纳里\n" +
                "赛诺\n" +
                "妮露\n" +
                "纳西妲\n" +
                "流浪者\n" +
                "艾尔海森\n" +
                "丽莎\n" +
                "凝光\n" +
                "凯亚\n" +
                "北斗\n" +
                "安柏\n" +
                "班尼特\n" +
                "砂糖\n" +
                "芭芭拉\n" +
                "菲谢尔\n" +
                "行秋\n" +
                "诺艾尔\n" +
                "重云\n" +
                "雷泽\n" +
                "香菱\n" +
                "迪奥娜\n" +
                "辛焱\n" +
                "罗莎莉亚\n" +
                "烟绯\n" +
                "早柚\n" +
                "九条裟罗\n" +
                "托马\n" +
                "五郎\n" +
                "云堇\n" +
                "久岐忍\n" +
                "鹿野院平藏\n" +
                "柯莱\n" +
                "多莉\n" +
                "坎蒂丝\n" +
                "莱依拉\n" +
                "珐露珊\n" +
                "瑶瑶";
        String[] names = nameStr.split("\n");
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CharacterDao characterDao = sqlSession.getMapper(CharacterDao.class);

        for (int pos=0;pos<names.length;pos++){
            Document doc = Jsoup.connect("https://wiki.biligame.com/ys/"+names[pos]).get();
            //System.out.println(doc.body());
            Element content = doc.getElementsByClass("wikitable").first();
            //System.out.println(content.text());
            String text = content.text();
            String[] textList = text.split(" ");
            for(int i=0;i<textList.length;i++){
                System.out.println(i + " " + textList[i]);
            }
            Character character = new Character();
            character.setName(names[pos]);
            character.setNationality(textList[5]);
            if("女".equals(textList[7])){
                character.setSex(2);
            }else {
                character.setSex(1);
            }
            if(textList[12].contains("雷")){
                character.setElementType("雷元素");
            }else if(textList[12].contains("风")){
                character.setElementType("风元素");
            }else if(textList[12].contains("火")){
                character.setElementType("火元素");
            }else if(textList[12].contains("水")){
                character.setElementType("水元素");
            }else if(textList[12].contains("岩")){
                character.setElementType("岩元素");
            }else if(textList[12].contains("草")){
                character.setElementType("草元素");
            }else if(textList[12].contains("冰")){
                character.setElementType("冰元素");
            }
            if(textList[14].contains("单手剑")){
                character.setWeaponType("单手剑");
            }else if(textList[14].contains("双手剑")){
                character.setWeaponType("双手剑");
            }else if(textList[14].contains("法器")){
                character.setWeaponType("法器");
            }else if(textList[14].contains("弓")){
                character.setWeaponType("弓");
            }else if(textList[14].contains("长柄武器")){
                character.setWeaponType("长柄武器");
            }
            character.setLabel(textList[22]);

            characterDao.insertSelective(character);
        }

        sqlSession.commit();
        System.out.println("ok");
    }
}
